#!/bin/sh -e
# Copyright (c) 2016 The crouton Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
REQUIRES=''
DESCRIPTION='Performs core system configuration. Most users would want this.'
CHROOTBIN='brightness croutonpowerd croutonversion host-dbus host-wayland'
. "${TARGETSDIR:="$PWD"}/common"

### Append to prepare.sh:
echo 'Preparing environment...' 1>&2
if [ "$VERSION" != '#VERSION#' ]; then
    sed -e "s ^VERSION=.*\$ VERSION='$VERSION' ;" \
        -e "s ^RELEASE=.*\$ RELEASE='$RELEASE' ;" \
        -e "s ^ARCH=.*\$ ARCH='$ARCH' ;" \
        -i '/usr/local/bin/croutonversion'
fi

# Create the new environment file
oldenv='/etc/environment'
newenv='/etc/environment.new'
{
    echo '### begin crouton-generated environment variables'
    if [ "$PROXY" = 'unspecified' -o "$PROXY" = '#PROXY#' ]; then
        # Copy over previously generated content
        awk '/^### end/ { exit }
             x && tolower($0) ~ /^[a-z]*_proxy=/
             /^### begin/ { x=1 }' "$oldenv" 2>/dev/null || true
    elif [ -n "$PROXY" ]; then
        for var in http_proxy HTTP_PROXY https_proxy HTTPS_PROXY \
                   ftp_proxy FTP_PROXY; do
            echo "$var='$PROXY'"
        done
        for var in no_proxy NO_PROXY; do
            echo "$var='localhost,127.0.0.1'"
        done
    fi
    echo '### end crouton-generated environment variables'
} > "$newenv"

# Set proxy variables for this script
. "$newenv"
export http_proxy https_proxy ftp_proxy

# Copy in previous user-environment settings
if [ -r "$oldenv" ]; then
    awk '/^### begin/{x=1}!x;/^### end/{x=0}' "$oldenv" >> "$newenv"
fi
mv -f "$newenv" "$oldenv"

if [ "${DISTROAKA:-"$DISTRO"}" = 'debian' ]; then
    echo 'Preparing software sources...' 1>&2
    if [ "$PROXY" != 'unspecified' -a "$PROXY" != '#PROXY#' ]; then
        aptproxy='/etc/apt/apt.conf.d/80croutonproxy'
        if [ -z "$PROXY" ]; then
            rm -f "$aptproxy"
        else
            cat > "$aptproxy" <<EOF
Acquire::http::proxy "$PROXY";
Acquire::ftp::proxy "$PROXY";
Acquire::https::proxy "$PROXY";
EOF
        fi
    fi

    # Only update sources.list if MIRROR is specified
    if [ -n "$MIRROR" -a "$MIRROR" != 'unspecified' ]; then
        if [ "$DISTRO" = 'ubuntu' ]; then
            # Ubuntu has its own categories of packages
            cat > /etc/apt/sources.list <<EOF
deb $MIRROR $RELEASE main restricted universe multiverse
deb-src $MIRROR $RELEASE main restricted universe multiverse
deb $MIRROR $RELEASE-updates main restricted universe multiverse
deb-src $MIRROR $RELEASE-updates main restricted universe multiverse
deb $MIRROR $RELEASE-security main restricted universe multiverse
deb-src $MIRROR $RELEASE-security main restricted universe multiverse
EOF
        else
            # Not all Debian distros have all sets of repos
            cat > /etc/apt/sources.list <<EOF
deb $MIRROR $RELEASE main non-free contrib
deb-src $MIRROR $RELEASE main non-free contrib
EOF
            # Only stable and testing have an updates channel
            if release -lt jessie; then
                cat >> /etc/apt/sources.list <<EOF
deb $MIRROR $RELEASE-updates main non-free contrib
deb-src $MIRROR $RELEASE-updates main non-free contrib
EOF
            fi
            # Some derivatives also have security updates
            if release -lt jessie -eq kali; then
                cat >> /etc/apt/sources.list <<EOF
deb $MIRROR2 $RELEASE/updates main non-free contrib
deb-src $MIRROR2 $RELEASE/updates main non-free contrib
EOF
            fi
        fi
    fi

    # Update the package list.
    # Ignore failures, as most are due to bad PPAs and are not critical.
    apt-get -y update || true

    echo 'Ensuring system is up-to-date...' 1>&2
    apt-get -y dist-upgrade
fi

# On release upgrade, keyboard-configuration might be reconfigured.
fixkeyboardmode

# Install critical packages
install --minimal sudo wget ca-certificates apt-transport-https

# Generate and set default locale
if ! grep -q '^[^#]*LANG=' /etc/default/locale 2>/dev/null && hash locale-gen 2>/dev/null; then
    echo 'LANG=en_US.UTF-8' > '/etc/default/locale'
    locale-gen --lang en_US.UTF-8
    if [ "${DISTROAKA:-"$DISTRO"}" = 'debian' ]; then
        dpkg-reconfigure locales
    fi
fi

# Link debian_chroot to the chroot name
if [ "${DISTROAKA:-"$DISTRO"}" = 'debian' ]; then
    ln -sfT '/etc/crouton/name' '/etc/debian_chroot'
fi

echo 'Syncing timezone...' 1>&2
# Link the timezone to Chromium OS
# Remove /etc/timezone: this tells Ubuntu/Debian that we are managing the
# content of /etc/localtime manually, and that it should not erase the symbolic
# link upon update, unless "dpkg-reconfigure tzdata" is called explicitly.
rm -f /etc/timezone

# /var/host/timezone/localtime is itself a symbolic link, but as long as the
# zoneinfo packages in the chroot and Chromium OS are the same, it'll be fine
ln -sfT /var/host/timezone/localtime /etc/localtime

# Link /etc/mtab to /proc/mounts. It's not totally accurate, but close enough,
# as it at least has / and all the media-mounted devices.
ln -sfT /proc/mounts /etc/mtab
